package com.netty.junjie.socket.impl;

import com.netty.junjie.socket.decoder.ConversionDecoder;
import com.netty.junjie.socket.handler.NettyServerHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * *
 * NettyChannelInitializerImpl 业务Handler
 *
 * @author JunJie
 * @Date 2020/07/13 07:13
 **/
@Component
@Slf4j
public class NettyChannelInitializerImpl extends ChannelInitializer<SocketChannel> {
    @Resource
    private NettyServerHandler nettyServerHandler;

    @Value("${netty.nettyPort.port}")
    private int nettyPort;

    @Override
    protected void initChannel(SocketChannel ch) {
        int localPort = ch.localAddress().getPort();
        log.debug("Netty连接的端口号: " + localPort);
        if (nettyPort == localPort) {
            ch.pipeline().addLast(new ConversionDecoder());
        }
        //添加自己业务Handler
        ch.pipeline().addLast(nettyServerHandler);
    }
}
